﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 操作员年资金情况
    /// </summary>
    public class ServiceOperatorYearMoney : Benefit.Interface.View.IOperatorYearMoney
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorYearMoney(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        /// <summary>
        /// 得到交易日账户信息
        /// </summary>
        /// <param name="accountMoneys"></param>
        /// <returns></returns>
        public OperatorYearMoney GetOperatorYearMoney(int year, int operatorId)
        {
            var query = db.OperatorYearMoney.Where(a => a.OperatorId == operatorId).Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 初始化操作员资金信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        public void InitOperatorYearMoney(int year)
        {
            var operators = db.Operators.ToList();
            OperatorYearMoney money = new OperatorYearMoney();
            foreach (Models.Sys.Operator op in operators)
            {
                var moneys = db.OperatorMonthMoney.Where(a => a.Year == year).Where(a => a.OperatorId == op.Id).ToList();
                var queryExist = db.OperatorYearMoney.Where(a => a.OperatorId == op.Id).Where(a => a.Year == year).ToList();
                if (queryExist.Count() > 0)
                {
                    queryExist.First().AppendMargin = moneys.Sum(a => a.AppendMargin);
                    queryExist.First().AvailableMoney = moneys.Sum(a => a.AvailableMoney);
                    queryExist.First().BaseMargin = moneys.Sum(a => a.BaseMargin);
                    queryExist.First().CloseProfit = moneys.Sum(a => a.CloseProfit);
                    queryExist.First().Commission = moneys.Sum(a => a.Commission);
                    queryExist.First().DeliveryCommission = moneys.Sum(a => a.DeliveryMargin);
                    queryExist.First().Deposit = moneys.Sum(a => a.Deposit);
                    queryExist.First().DynamicMoney = moneys.Sum(a => a.DynamicMoney);
                    queryExist.First().FrozenCommission = moneys.Sum(a => a.FrozenCommission);
                    queryExist.First().FrozenMargin = moneys.Sum(a => a.FrozenMargin);
                    queryExist.First().LongPosition = moneys.Sum(a => a.LongPosition);
                    queryExist.First().Margin = moneys.Sum(a => a.Margin);
                    queryExist.First().Mortgage = moneys.Sum(a => a.Mortgage);
                    queryExist.First().PositionProfit = moneys.Sum(a => a.PositionProfit);
                    queryExist.First().Premortgage = moneys.Sum(a => a.Premortgage);
                    queryExist.First().PresetTlePosit = moneys.Sum(a => a.PresetTlePosit);
                    queryExist.First().RiskRate = moneys.Sum(a => a.RiskRate);
                    queryExist.First().SettlePosit = moneys.Sum(a => a.SettlePosit);
                    queryExist.First().ShortPosition = moneys.Sum(a => a.ShortPosition);
                    queryExist.First().WithDraw = moneys.Sum(a => a.WithDraw);
                    queryExist.First().WithDrawQuota = moneys.Sum(a => a.WithDrawQuota);
                    queryExist.First().Year = year;
                    queryExist.First().OperatorId = op.Id;
                }
                else
                {
                    OperatorYearMoney oym = new OperatorYearMoney();
                    oym.Year = year;
                    oym.OperatorId = op.Id;
                    oym.AppendMargin = moneys.Sum(a => a.AppendMargin);
                    oym.AvailableMoney = moneys.Sum(a => a.AvailableMoney);
                    oym.BaseMargin = moneys.Sum(a => a.BaseMargin);
                    oym.CloseProfit = moneys.Sum(a => a.CloseProfit);
                    oym.Commission = moneys.Sum(a => a.Commission);
                    oym.DeliveryCommission = moneys.Sum(a => a.DeliveryMargin);
                    oym.Deposit = moneys.Sum(a => a.Deposit);
                    oym.DynamicMoney = moneys.Sum(a => a.DynamicMoney);
                    oym.FrozenCommission = moneys.Sum(a => a.FrozenCommission);
                    oym.FrozenMargin = moneys.Sum(a => a.FrozenMargin);
                    oym.LongPosition = moneys.Sum(a => a.LongPosition);
                    oym.Margin = moneys.Sum(a => a.Margin);
                    oym.Mortgage = moneys.Sum(a => a.Mortgage);
                    oym.PositionProfit = moneys.Sum(a => a.PositionProfit);
                    oym.Premortgage = moneys.Sum(a => a.Premortgage);
                    oym.PresetTlePosit = moneys.Sum(a => a.PresetTlePosit);
                    oym.RiskRate = moneys.Sum(a => a.RiskRate);
                    oym.SettlePosit = moneys.Sum(a => a.SettlePosit);
                    oym.ShortPosition = moneys.Sum(a => a.ShortPosition);
                    oym.WithDraw = moneys.Sum(a => a.WithDraw);
                    oym.WithDrawQuota = moneys.Sum(a => a.WithDrawQuota);
                    oym.Year = year;
                    oym.OperatorId = op.Id;
                    db.OperatorYearMoney.Add(oym);
                }
            }
            db.SaveChanges();
        }

        public void Delete(int year)
        {
            var query = db.OperatorYearMoney.Where(a => a.Year == year);
            foreach (OperatorYearMoney m in query)
            {
                db.OperatorYearMoney.Remove(m);
            }
            db.SaveChanges();
        }
    }
}
